package LeetCode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by kechelle on 2020/8/22 20:32
 * 只出现一次的数字
 */
public class Code136 {
    //位运算符方法
    public static int singleNumber(int[] nums) {
        int result = nums[0];
        for (int i=1;i<nums.length;i++){
            result = result ^ nums[i];
        }
        return result;
    }

    //借助HashMap
    public static int singleNumber1(int[] nums){
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int num:nums){
            if (map.containsKey(num)){
                map.put(num,map.get(num)+1);
            }else {
                map.put(num,1);
            }
        }
        for (Integer key:map.keySet()){
            if (map.get(key) == 1) return key;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {3,2,2,3,5};
        System.out.println(Code136.singleNumber1(nums));
    }
}
